Page({
    data: {
        address: {},
        cart: [],
        allChecked: true,
        totalPrice: 0,
        totalNum: 0,
        checkedGoods: [],
        totalLength: 0,
        checkLength: 0
    },
    onShow() {
        let that = this
        // 1 获取缓存中的收货地址信息
        let token = wx.getStorageSync("token")
        let len = 0
        wx.request({
            url: "https://www.cbing.icu:8095/wxUser/getUserCart",
            method: 'GET',
            header: {
                'Authorization': token
            },
            success(res) {
                console.log(res)
                let total = 0
                for (let i of res.data.data.userGoods) {
                    if (i.status === 1) {
                        total += i.goods.price * i.num
                        len += 1
                    } else {
                        that.setData({
                            allChecked: false
                        })
                    }
                }
                that.setData({
                    cart: res.data.data.userGoods,
                    totalPrice: total * 100,
                    totalLength: res.data.data.userGoods.length,
                    checkLength: len
                })
                console.log(res.data.data.userGoods)
            }
        })

    },
    // 点击 收货地址
    // 商品的选中
    handeItemChange(e) {
        let total = this.data.totalPrice
        // 1 获取被修改的商品的id
        let token = wx.getStorageSync("token")
        const goods_id = e.currentTarget.dataset.id;
        // 2 获取购物车数组
        let cart = this.data.cart;
        // 3 找到被修改的商品对象
        let index = cart.findIndex(v => v.goods.id === goods_id);
        // console.log(index)
        // // 4 选中状态取反
        // console.log(cart)
        if (cart[index].status === 1) {
            cart[index].status = 0;
            this.setData({
                totalPrice: total - 100 * cart[index].goods.price * cart[index].num,
                allChecked: false,
                checkLength: this.data.checkLength - 1
            })
        } else {
            cart[index].status = 1;
            let nowlen = this.data.checkLength + 1
            if (nowlen === this.data.totalLength) {
                this.setData({
                    allChecked: true
                })
            }
            this.setData({
                totalPrice: total + 100 * cart[index].goods.price * cart[index].num,
                checkLength: nowlen
            })
        }
        wx.request({
            url: "https://www.cbing.icu:8095/wxUser/goodChecked?goodId=" + cart[index].goods.id + "&status=" + cart[index].status,
            method: 'GET',
            header: {
                'Authorization': token
            },
            success(res) {
            }
        })
        this.setData({
            cart: cart
        })
    },
    onALLChange() {
        let token = wx.getStorageSync("token")
        let that = this
        let cart = that.data.cart
        if (that.data.allChecked === true) {
            for (let i in cart) {
                let good = cart[i].goods
                cart[i].status = 0
                wx.request({
                    url: "https://www.cbing.icu:8095/wxUser/goodChecked?goodId=" + good.id + "&status=" + 0,
                    method: 'GET',
                    header: {
                        'Authorization': token
                    },
                    success(res) {
                    }
                })
            }
            that.setData({
                cart: cart,
                checkLength: 0,
                totalPrice: 0
            })
        } else {
            let price = that.data.totalPrice
            for (let i in cart) {
                let good = cart[i].goods
                if (cart[i].status === 0) {
                    cart[i].status = 1
                    price += 100 * good.price * cart[i].num
                    wx.request({
                        url: "https://www.cbing.icu:8095/wxUser/goodChecked?goodId=" + good.id + "&status=" + 1,
                        method: 'GET',
                        header: {
                            'Authorization': token
                        },
                        success(res) {
                        }
                    })
                }

            }
            that.setData({
                cart: cart,
                checkLength: that.data.totalLength,
                totalPrice: price
            })
        }
        that.setData({
            allChecked: !that.data.allChecked
        })

    },
    handleItemNumEdit(e) {
        let that = this
        let token = wx.getStorageSync("token")
        let total = this.data.totalPrice
        // 1 获取被修改的商品的id
        // 2 获取购物车数组
        let {cart} = that.data
        let op = e.currentTarget.dataset.operation
        let goodid = e.currentTarget.dataset.id
        let index = cart.findIndex(v => v.goods.id === goodid);
        if (cart[index].num + op === 0) {
            wx.showModal({
                content: '确定要将该商品移出购物车吗?',
                confirmText: '确定',
                cancelText: '取消',
                success: function (res) {
                    if (res.cancel) {

                    } else {
                        wx.request({
                            url: "https://www.cbing.icu:8095/wxUser/deleteGood?goodId=" + goodid,
                            method: 'GET',
                            header: {
                                'Authorization': token
                            },
                            success(res) {
                                wx.showToast({
                                    title: res.data.message
                                })
                                if (res.data.code !== 200) {

                                } else {
                                    if (cart[index].status === 1) {
                                        that.setData({
                                            totalPrice: total + op * 100 * cart[index].goods.price
                                        })
                                    }
                                    cart.splice(index, 1)
                                    that.setData({
                                        cart: cart,
                                        totalLength: that.data.totalLength - 1,
                                    })
                                    if (that.data.totalLength === that.data.checkLength) {
                                        that.setData({
                                            allChecked: true
                                        })
                                    }
                                }
                                // if(res.data.)
                            }
                        })
                    }
                }
            })

        } else {
            cart[index].num += op
            wx.request({
                url: "https://www.cbing.icu:8095/wxUser/changeGoodNum?goodId=" + goodid + "&num=" + cart[index].num,
                method: 'GET',
                header: {
                    'Authorization': token
                },
                success(res) {
                    if (res.data.code !== 200) {
                        wx.showToast({
                            title: "添加失败！"
                        })
                    } else {
                        if (cart[index].status === 1) {
                            that.setData({
                                totalPrice: total + op * 100 * cart[index].goods.price
                            })
                        }
                        that.setData({
                            cart: cart
                        })
                    }
                    // if(res.data.)
                }
            })
        }

    },
    onClickButton() {
        let that = this
        if(that.data.totalLength===0||that.data.checkLength===0){
            wx.showToast({
                title:"您还没有选择商品",
                duration:2000,
                icon:'none'
            })
        }else {
            wx.setStorageSync("cart",that.data.cart)
            wx.setStorageSync("total",that.data.totalPrice)
            wx.navigateTo({
                url: "../order/index",
                success: (res) => {
                    // 通过 eventChannel 向被打开页面传送数据
                    res.eventChannel.emit('getOrder', {
                        data: that.data.cart
                    })
                }
            })

        }


    }
})